package com.suneee.store.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.base.util.ContextHelper;
import com.suneee.context.page.Page;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.store.bean.ViewShopStockRecordsDTO;
import com.suneee.store.dao.ViewShopStockRecordsDao;
import com.suneee.store.entity.ViewShopStockRecords;

@Repository("viewShopStockRecordsDao")
public class ViewShopStockRecordsDaoImpl extends
		GenericDao<ViewShopStockRecords, Integer> implements
		ViewShopStockRecordsDao {

	@Override
	public Page<ViewShopStockRecords> listView(ViewShopStockRecordsDTO dto) {
		// TODO Auto-generated method stub
		WhereClauseBuilder sbf = this.wrapWhereSQL(dto);
		return this.executeQuery(sbf.getSql(), dto, sbf.getArgs());
	}

	private WhereClauseBuilder wrapWhereSQL(ViewShopStockRecordsDTO dto) {
		WhereClauseBuilder sbf = new WhereClauseBuilder(
				"select * from v_shop_stock_records where 1=1 ");
		sbf.equals("shop_id", dto.getShopId());
		sbf.equals("enterprise_code", dto.getEnterpriseCode());
		sbf.equals("category_id", dto.getCategoryId());
		sbf.contains("title", dto.getTitle());
		sbf.equals("status", dto.getStatus());
		sbf.equals("record_type", dto.getRecordType());
		sbf.equals("operate_store", dto.getOperateStore());
		sbf.equals("spu_id", dto.getSpuId());
		sbf.equals("sku_id", dto.getSkuId());
		sbf.equals("record_id", dto.getRecordId());
		sbf.equals("stock_type", dto.getStockType());
		sbf.equals("create_user_name", dto.getCreateUser());
		sbf.between("create_time", dto.getCreateTimeStart(),
				dto.getCreateTimeEnd());
		sbf.in("stock_id", dto.getStockIdList());
		return sbf;
	}

	@Override
	public Page<Map<String, Object>> mapListView(ViewShopStockRecordsDTO dto) {
		// TODO Auto-generated method stub
		WhereClauseBuilder sbf = this.buildWhereSQL(dto);
		return this.queryForList(sbf.getSql(), dto, sbf.getArgs());
	}

	private WhereClauseBuilder buildWhereSQL(ViewShopStockRecordsDTO dto) {
		List<Object> list = null;
	//	if ("店铺出库".equals(dto.getRecordType())) {
			Integer adminId = ContextHelper.getCurrentLoginInfo().getAdminId();
			List<Map<String, Object>> shopList = this.queryForList(
					"select shop_id from t_shop_admin where admin_id = ?",
					adminId);
			if (null != shopList && shopList.size() > 0) {
				list = new ArrayList<Object>();
				for (Map<String, Object> map : shopList) {
					list.add(map.get("shop_id"));
				}
			}
			if (null == shopList || shopList.size() == 0) {
				list = new ArrayList<Object>();
				list.add(" ");
			}

	//	}

		String sql = "SELECT "
				+ "re.record_id AS recordId,"
				+ "re.operate_store_id AS operateStoreId,"
				+ "re.relevant_store_id AS relevantStoreId,"
				+ "re.record_type AS recordType,"
				+ "re.create_time AS createTime,"
				+ "re.remark,"
				+ "re.stock_type AS stockType,"
				+ "re.channel_code AS channelCode,"
				+ "re.operate_channel_code AS operateChannelCode,"
				+ "re.relevant_channel_code AS relevantChannelCode,"
				+ "cha.`name` AS channelName,"
				+ "sp.`name` AS operateStoreName,"
				+ "spp.`name` AS relevantStoreName,"
				+ "de.spu_id AS spuId,"
				+ "de.sku_id AS skuId,"
				+ "de.price AS price,"
				//+ "de.price AS advicePrice,"
				+ "de.incoming AS stock,"
				+ "spu.title AS title,"
				+ "spu.market_price AS marketPrice,"
				+ "sku.spec_one_value AS specOneValue,"
				+ "sku.spec_two_value AS specTwoValue,"
				+ "sku.spec_three_value AS specThreeValue,"
				+ "sku.spec_four_value AS specFourValue,"
				+ "adm.account AS createUserName,"
				+ "cat.`name` AS categoryName"
				+ " FROM"
				+ " t_stock_record re"
				+ " LEFT JOIN t_shop sp  ON sp.shop_id = re.operate_store_id "
				+ " LEFT JOIN t_shop spp ON spp.shop_id = re.relevant_store_id"
				+ " LEFT JOIN t_stock_detail de on de.record_id = re.record_id"
				+ " LEFT JOIN t_spu spu on spu.spu_id = de.spu_id"
				+ " LEFT JOIN t_sku sku on sku.sku_id = de.sku_id"
				+ " LEFT JOIN t_category cat on cat.category_code = spu.category_id"
				+ " LEFT JOIN t_administrator adm on adm.admin_id =  re.create_user_id "
				+ " LEFT JOIN t_channel cha on cha.channel_code = re.channel_code"
				+ " WHERE  re.record_id = de.record_id "
				+ " and  re.operate_store_id = sp.shop_id "
				+ " and  re.relevant_store_id = spp.shop_id"
				+ " AND de.spu_id = spu.spu_id" + " AND de.sku_id = sku.sku_id"
				+ " AND spu.category_id = cat.category_code"
				+ " AND re.create_user_id = adm.admin_id "
				+ " AND re.channel_code = cha.channel_code ";

		WhereClauseBuilder sbf = new WhereClauseBuilder(sql);
		sbf.equals("re.channel_code", dto.getChannelCode());
		sbf.equals("re.shop_id", dto.getShopId());
		
		if("店铺入库".equals(dto.getRecordType())){
			sbf.in("spp.shop_id", list);
			sbf.equals("re.relevant_store_id", dto.getRelevantStoreId());
		}else{
			sbf.in("sp.shop_id", list);
		}
		
		sbf.equals("re.enterprise_code", dto.getEnterpriseCode());
		sbf.equals("spu.category_id", dto.getCategoryId());
		sbf.contains("spu.title", dto.getTitle());
		sbf.equals("de.status", dto.getStatus());
		sbf.equals("re.record_type", dto.getRecordType());
		
		sbf.equals("de.spu_id", dto.getSpuId());
		sbf.equals("de.sku_id", dto.getSkuId());
		sbf.equals("re.record_id", dto.getRecordId());
		sbf.equals("re.stock_type", dto.getStockType());
		sbf.equals("adm.account", dto.getCreateUser());
		sbf.equals("re.operate_store_id", dto.getOperateStoreId());
		sbf.between("re.create_time", dto.getCreateTimeStart(),
				dto.getCreateTimeEnd());
		Object[] obj = null;
		sbf.append(" ORDER BY re.create_time DESC ", obj);
		return sbf;
	}
}
